home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 10778 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  1.3 KB

  1. Path: news.axess.com!news
  2. From: tdrymona@axess.com (Kamikaze)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: Help: File counting lines
  5. Date: Sun, 10 Mar 1996 17:18:04 GMT
  6. Organization: Axess Communications, Montreal, Canada
  7. Message-ID: <4huog1$t8s@news.axess.com>
  8. References: <4hni12$qst@bolivia.it.earthlink.net>
  9. Reply-To: tdrymona@axess.com
  10. NNTP-Posting-Host: cretien.axess.com
  11. X-Newsreader: Forte Free Agent v0.55
  12.  
  13. brunop@earthlink.net (Peter Bruno) wrote:
  14.  
  15. >Does anyone have any suggestions on a fast way to count the number of lines in 
  16. >a ASCII file.  Each line is terminted by a <enter> and all I need to do is 
  17. >count the number of lines (records) in the file.
  18.  
  19. >The way I've been doing it is by:  fgets(row, 128, File); count++;
  20. >however, if the line is longer then 128 characters this does not work and it 
  21. >would seem that there must be a better way of doing it anyway... perhaps by 
  22. >incrementing the pointer until EOF??
  23.  
  24. >Any assistance would be greatly appreciated...
  25.  
  26. >Thanx,
  27.  
  28. >Peter Bruno
  29. >brunop@earthlink.net
  30.  
  31. Yeah use the stream objects, like this
  32.  
  33. ifstream in;
  34. int count = 0;
  35. char buffer[BUFFER_SIZE );
  36.  
  37. in = open( name_of_file );
  38. if ( !in.bad() )
  39. {
  40.     while ( !in.eof() ) 
  41.     {
  42.     getline( buffer, sizeof( buffer ) );
  43.     count++;
  44.     }
  45.     in.close();    
  46. }
  47.  
  48.  
  49. Something like this, you get the picture.
  50.  
  51.  
  52.